package com.hw.blog.controller;

import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hw.blog.error.RequiredLoginException;

public abstract class BaseController {
	protected Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * 관리자 로그인 여부 true, false 반환
	 * @param request
	 * @return
	 * @throws Exception
	 */
	protected boolean isAdminLogin(HttpServletRequest request) throws Exception {
		
		Map usr = (Map) request.getSession().getAttribute("userInfo");
		if( null == usr || "".equals(usr.get("admin_id"))){
			return false;
		}else{
			
			return true;
		}
	}
	
	/**
	 * 로그인 없이 들어온 사용자 다른페이지로 리턴
	 * @param req
	 * @param res
	 * @throws Exception
	 */
	protected void isAdminMember(HttpServletRequest req,
			HttpServletResponse res) throws Exception{
		/* 로그인 여부 검사*/
		if (!isAdminLogin(req) ) {
			String url1 = URLEncoder.encode(req.getRequestURI().replace(req.getContextPath(), ""),"UTF-8");
			String param = requestPram2Str(req);
//			System.out.println(url1);
//			System.out.println(param);
			
			String url = req.getRequestURI().replace(req.getContextPath(), "");
			String p_id=req.getParameter("p_id");
//			throw new RequiredLoginException(p_id);
			throw new RequiredLoginException(p_id,"/pmgr/pe/ti/peti1100Q.do","/pmgr/pe/ti/peti100Q.do");
		}
	}
	
	/**
	 * 펜션 관리자 로그인 여부 true false 반환
	 * @param request
	 * @return
	 * @throws Exception
	 */
	protected boolean isLogin(HttpServletRequest request) throws Exception {
		
		
		Map usr = (Map) request.getSession().getAttribute("userInfo");
		
		if( null == usr || "".equals(usr.get("p_id"))){
			return false;
		}else{
			//관리자가 아니고
			if(("pmgr".equals(usr.get("admin_id")) || request.getParameter("p_id").equals(usr.get("p_id")))){
				return true;	
			}else{
				request.getSession().removeAttribute("userInfo");
				request.getSession().removeAttribute("smsChk");
				return false;
			}
		}
	}

	/**
	 * 펜션 관리자 로그인 없이 들어온사용자 다른페이지로 리턴
	 * @param req
	 * @param res
	 * @throws Exception
	 */
	protected void isMember(HttpServletRequest req,
			HttpServletResponse res) throws Exception{
//		Map user = (Map) req.getSession().getAttribute("userInfo");
		/* 로그인 여부 검사*/
		if (!isLogin(req) ) {
//			String url = URLEncoder.encode(req.getRequestURI().replace(req.getContextPath(), ""),"UTF-8");
			String param = requestPram2Str(req);
			String url = req.getRequestURI().replace(req.getContextPath(), "");
			String p_id=req.getParameter("p_id");
			throw new RequiredLoginException(p_id,"/usr/pe/re/pere100Q.do?p_id="+p_id,"/usr/pe/re/pere100Q.do?p_id="+p_id);
		}
	}
	
	public String getReg_nm(HttpServletRequest request) {
		Map usr = (Map) request.getSession().getAttribute("userInfo");
		return (String) usr.get("ADMIN_NM");
	}
	
	public int getAdmin_seq_no(HttpServletRequest request) {
		Map usr = (Map) request.getSession().getAttribute("userInfo");
		return Integer.parseInt( String.valueOf( usr.get("ADMIN_SEQ_NO")));
	}

	public String requestPram2Str(HttpServletRequest httprequest) {

		Enumeration enum1 = httprequest.getParameterNames();

		int count = httprequest.getParameterMap().size();
		if(0==count)return "";

		StringBuffer ret=new StringBuffer("?");;

		for (int i = 0; i < count; i++) {
			if(i!=0){
				ret.append("&");
			}
			String cd = (String)enum1.nextElement();
			
			ret.append(cd);
			ret.append("=");
			ret.append(httprequest.getParameter(cd));
			
		}

		return ret.toString();
	}
	
	public Map requestPram2Map(HttpServletRequest httprequest) {

		Enumeration enum1 = httprequest.getParameterNames();

		int count = httprequest.getParameterMap().size();
		Map rtMap = new HashMap();
		if(0==count)return rtMap;
		
		for (int i = 0; i < count; i++) {
			
			String cd = (String)enum1.nextElement();
			rtMap.put(cd, httprequest.getParameter(cd));
		}

		return rtMap;
	}

}
